package handler

import (
	"context"
	"fmt"
	comm "goproduct/common"
	"goproduct/domain/model"
	"goproduct/domain/service"
	"goproduct/proto"
)

type ProductHandler struct {
	ProductDataService service.IProductDataService
}

// 查询商品列表
func (u *ProductHandler) Page(ctx context.Context, req *proto.PageReq, resp *proto.PageResp) error {
	//count := u.ProductDataService.CountNum()
	count, obj, err := u.ProductDataService.Page(req.GetLength(), req.GetPageIndex())
	if err != nil {
		println("page product err :", err)
	}
	//count = u.ProductDataService.CountNum()
	resp.Rows = int64(req.GetLength())
	resp.Total = count
	//fmt.Println(">>>>>>>>>>>>> page product success :", obj)
	ObjForResp(obj, resp)
	return nil
}

/*
*

	"id": 115,
	               "name": "马歇尔MARSHALL STANMOREⅡ无线蓝牙音响家用复古重低音小音箱",
	               "startingPrice": 3300,
	               "mainPicture": "https://msb-edu-prod.oss-cn-beijing.aliyuncs.com/mall-product/product/d4f1f19e-2e90-4ac5-bbe3-1eba02085a0f.jpg",
	               "labelList": [ ],
	               "singleBuyLimit": null,
	               "isEnable": null,
	               "productType": null

*
*/
func ObjForResp(obj *[]model.Product, resp *proto.PageResp) (err error) {
	for _, v := range *obj {
		product := &proto.Product{}
		err := comm.SwapToStruct(v, product)
		if err != nil {
			return err
		}
		fmt.Println(">>>>>>>>>>>>> ", product)
		resp.Product = append(resp.Product, product)
	}
	return nil
}

// 商品详情
func (u *ProductHandler) ShowProductDetail(ctx context.Context, req *proto.ProductDetailReq, resp *proto.ProductDetailResp) error {
	//count := u.ProductDataService.CountNum()
	obj, err := u.ProductDataService.ShowProductDetail(req.GetId())
	if err != nil {
		println("ShowProductDetail   err :", err)
	}
	productDetail := &proto.ProductDetail{}
	err1 := comm.SwapToStruct(obj, productDetail)
	if err1 != nil {
		println("ShowProductDetail SwapToStruct  err :", err1)
	}
	resp.ProductDetail = append(resp.ProductDetail, productDetail)
	return nil
}

// 商品SKU列表
func (u *ProductHandler) ShowProductSku(ctx context.Context, req *proto.ProductSkuReq, resp *proto.ProductSkuResp) error {
	//count := u.ProductDataService.CountNum()
	obj, err := u.ProductDataService.ShowProductSku(req.GetProductId())
	if err != nil {
		println("ShowProductSku   err :", err)
	}
	err1 := ObjSkuForResp(obj, resp)
	if err1 != nil {
		println("ShowProductSku SwapToStruct  err :", err1)
	}
	return nil
}

func ObjSkuForResp(obj *[]model.ProductSku, resp *proto.ProductSkuResp) (err error) {
	for _, v := range *obj {
		productSku := &proto.ProductSku{}
		err := comm.SwapToStruct(v, productSku)
		if err != nil {
			return err
		}
		resp.ProductSku = append(resp.ProductSku, productSku)
	}
	return nil
}

// 商品SKU详情
func (u *ProductHandler) ShowDetailSku(ctx context.Context, req *proto.ProductDetailReq, resp *proto.ProductSkuResp) error {
	//count := u.ProductDataService.CountNum()
	obj, err := u.ProductDataService.ShowDetailSku(req.Id)
	if err != nil {
		println("ShowDetailSku   err :", err)
	}
	productSku := &proto.ProductSku{}
	err = comm.SwapToStruct(obj, productSku)
	if err != nil {
		return err
	}
	resp.ProductSku = append(resp.ProductSku, productSku)
	return nil
}

// 修改商品SKU
func (u *ProductHandler) UpdateSku(ctx context.Context, req *proto.UpdateSkuReq, resp *proto.UpdateSkuResp) error {
	//count := u.ProductDataService.CountNum()
	isSuccess, err := u.ProductDataService.UpdateSku(req)
	if err != nil {
		resp.IsSuccess = isSuccess
		println("UpdateSku   err :", err)
	}
	resp.IsSuccess = isSuccess
	return err
}
